package org.clipjoint.service.impl;

import org.clipjoint.dao.GameAdminDao;
import org.clipjoint.dao.impl.GameAdminDaoImpl;
import org.clipjoint.entity.dto.GameDto;
import org.clipjoint.entity.Game;
import org.clipjoint.service.GameAdminService;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

/**
 * @author 陈光耀
 * @date 2021/2/22
 */
public class GameAdminServiceImpl implements GameAdminService {

    private GameAdminDao gameDao = new GameAdminDaoImpl();

    @Override
    public List<Game> list(GameDto gameDto) throws SQLException {
        return gameDao.selectAll(gameDto);
    }

    @Override
    public Long count(GameDto gameDto) throws SQLException {
        return gameDao.selectCount(gameDto);
    }

    @Override
    public int add(Game game) throws SQLException {
        game.setTime(new Date());
        return gameDao.insert(game);
    }

    @Override
    public int deleteAll(List<Long> ids) {
        int result = 0;
        for (Long id : ids) {
            try {
                result += gameDao.deleteById(id);
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }

        return result;
    }

    @Override
    public Game getOneById(Long id) throws SQLException {
        Game game = gameDao.selectOne(GameDto.builder().id(id).build());
        // 查询游戏主图的临时url地址
//        if (StringUtils.isNotBlank(game.getImage())) {
//            game.setImageTempUrl(AliyunOSSUtil.getTempUrl(game.getImage()).toString());
//        }
        // 将游戏内容中所有的src的objectName替换成临时url地址
        // (?<=src=")[^"]+(?=") 匹配所有src的属性值
        // 1.创建正则表达式对象
        //Pattern pattern = Pattern.compile("(?<=src=\")[^\"]+(?=\")");
        // 2.创建匹配器对象
        //String content = game.getContent();
        //Matcher matcher = pattern.matcher(content);
        // 3.查找匹配结果
        //while (matcher.find()) {
        // 获取匹配的内容
        //String objectName = matcher.group();
        // 将objectName替换成临时url地址
        //content = content.replace(objectName, AliyunOSSUtil.getTempUrl(objectName).toString());
        //}

        //game.setContent(content);

        return game;
    }


    @Override
    public int delete(Long id) throws SQLException {
        return gameDao.deleteById(id);
    }

    @Override
    public int update(Game game) throws SQLException {

        return gameDao.update(game);
    }

}
